﻿using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using PrismApp.MsgEvent;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrismApp.ViewModels
{
    public class CommandWindowViewModel : BindableBase
    {
        #region 1.常规命令
        private DelegateCommand<string> _changeValue;
        public DelegateCommand<string> ChangeValueCommand
        {
            get
            {
                // 第一种情况，传统方式挂载执行委托、检查可执行委托
                // 需要配合ChangeValue.RaiseCanExecuteChanged();
                //if (_changeValue == null)
                //    _changeValue = new DelegateCommand(DoChangeValue, DoCanChangeValue);

                // 第二种方式，使用DelegateCommand对象的ObservesProperty方法
                // 不需要ChangeValue.RaiseCanExecuteChanged();
                //if (_changeValue == null)
                //{
                //    _changeValue = new DelegateCommand(DoChangeValue, DoCanChangeValue);
                //     
                //                 观察一个属性，当这个属性变化的时候触发DoCanChangeValue
                //    _changeValue.ObservesProperty(() => IsCan);
                //}

                // 第三种方式 通过ObservesCanExecute观察一个属性，属性必须是一个bool型
                //if (_changeValue == null)
                //{
                //    _changeValue = new DelegateCommand(DoChangeValue);
                //    _changeValue.ObservesCanExecute(() => IsCan);
                //}

                // 第四种方式 带参数
                if (_changeValue == null)
                {
                    _changeValue = new DelegateCommand<string>(async (o) => await DoChangeValue(o));
                    _changeValue.ObservesCanExecute(() => IsCan);
                }

                return _changeValue;
            }
        }

        private DelegateCommand<object> _changeValueStatusControlOneCommand;
        public DelegateCommand<object> ChangeValueStatusControlOneCommand
        {
            get
            {
                if (_changeValueStatusControlOneCommand == null)
                {
                    _changeValueStatusControlOneCommand = new DelegateCommand<object>(ChangeValue, DoCanChangeValue);
                }
                return _changeValueStatusControlOneCommand;
            }
        }

        private DelegateCommand<string> _changeValueStatusControlTwoCommand;
        public DelegateCommand<string> ChangeValueStatusControlTwoCommand
        {
            get
            {
                return _changeValueStatusControlTwoCommand;
            }
        }

        private DelegateCommand<string> _changeValueStatusControlThreeCommand;
        public DelegateCommand<string> ChangeValueStatusControlThreeCommand
        {
            get
            {
                return _changeValueStatusControlThreeCommand;
            }
        }

        #endregion

        #region 3.事件聚合器相关
        private DelegateCommand _command;
        public DelegateCommand Command
        {
            get
            {
                if (_command == null)
                {
                    _command = new DelegateCommand(() =>
                    {
                        // 弹出窗口
                        // 自定义窗口
                        // Messenger  Token   Type

                        //_ea.GetEvent<PubSubEvent<string>>().Publish("123");
                        //_ea.GetEvent<MessageEvent>().Publish("123");

                        var value = _ea.GetEvent<MessageEvent>().MyProperty;
                        _ea.GetEvent<MessageEvent1>().Publish("123");
                        _ea.GetEvent<MessageEvent1>().Publish("456");
                        _ea.GetEvent<MessageEvent1>().Publish("123");
                        //Task.Run(() =>
                        //{
                        //    _ea.GetEvent<MessageEvent1>().Publish("123");
                        //});
                    });
                }
                return _command;
            }
        }
        #endregion

        #region 2.用于事件绑定的命令
        /// <summary>
        /// 用于事件绑定的命令
        /// </summary>
        private DelegateCommand<object> _eventCommand;
        public DelegateCommand<object> EventCommand
        {
            get
            {
                if (_eventCommand == null)
                {
                    _eventCommand = new DelegateCommand<object>((o) =>
                    {

                    });
                }
                return _eventCommand;
            }
        }
        #endregion

        private string _value;
        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                SetProperty<string>(ref _value, value);
            }
        }

        private ObservableCollection<string> _valueList;
        public ObservableCollection<string> ValueList
        {
            get
            {
                return _valueList ?? (_valueList = new ObservableCollection<string>());
            }
            set
            {
                _valueList = value;
            }
        }

        private bool _isCan;
        public bool IsCan
        {
            get { return _isCan; }
            set
            {
                SetProperty(ref _isCan, value);
                ChangeValueCommand.RaiseCanExecuteChanged();
            }
        }

        IEventAggregator _ea;
        /// <summary>
        /// Ctor
        /// </summary>
        public CommandWindowViewModel(IEventAggregator ea, IContainerExtension containerRegistry)
        {
            //_ea = ea;
            ////订阅一个消息事件
            ////ea.GetEvent<PubSubEvent<string>>().Subscribe(DoMessage);
            //var me = ea.GetEvent<MessageEvent>();
            //me.Subscribe(DoMessage);
            //me.MyProperty = 123;

            ///// ThreadOption，，默认PublisherThread，管理订立消息的执行线程
            ///// keepSubscriberReferenceAlive：订阅事件属于一个弱引用
            ///// Filter  消息过滤 ，如果回调返回True，才执行消息体，否则不处理此消息
            //ea.GetEvent<MessageEvent1>().Subscribe(DoMessage1, ThreadOption.PublisherThread, keepSubscriberReferenceAlive: false, DoFilter);

            ////取消订立，如果 keepSubscriberReferenceAlive 为False或默认值 不需要取消订阅
            ////ea.GetEvent<MessageEvent1>().Unsubscribe(DoMessage1);
        }

        private void DoMessage(string msg)
        {

        }

        private void DoMessage1(string msg)
        {
            ValueList.Add(msg);
        }

        private bool DoFilter(string msg)
        {
            return msg.Contains("12");
        }

        /// <summary>
        /// 判断命令相关按钮是否可执行
        /// </summary>
        /// <returns></returns>
        private bool DoCanChangeValue(object obj)
        {
            //当属性Value的值等于1231的时候，按钮可点击
            return Value == "1231";
        }

        /// <summary>
        /// 命令执行体
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private async Task DoChangeValue(string param)
        {
            await Task.Delay(1000);
            Value = "456";
        }

        /// <summary>
        /// 命令执行体
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        private void ChangeValue(object param)
        {
           
            Value = "456";
        }
    }
}
